

#ifndef PF_ARC_H
#define PF_ARC_H

#include "pf_atomicentity.h"
#include "lx_Export.h"

/**
 * Holds the data that defines an arc.
 */
class LX_EXPORT PF_ArcAttribute {
public:
    PF_ArcAttribute() {}

    PF_ArcAttribute(const PF_Vector& center,
               double radius,
               double angle1, double angle2,
               bool reversed) {

        this->center = center;
        this->radius = radius;
        this->angle1 = angle1;
        this->angle2 = angle2;
        this->reversed = reversed;
    }

    void reset() {
        center = PF_Vector(false);
        radius = 0.0;
        angle1 = 0.0;
        angle2 = 0.0;
        reversed = false;
    }

    bool isValid() {
        return (center.valid && radius>PF_TOLERANCE &&
                fabs(angle1-angle2)>PF_TOLERANCE_ANGLE);
    }

    friend std::ostream& operator << (std::ostream& os, const PF_ArcAttribute& ad) {
        os << "(" << ad.center <<
        "/" << ad.radius <<
        " " << ad.angle1 <<
        "," << ad.angle2 <<
        ")";
        return os;
    }

public:
    PF_Vector center;
    double radius;
    double angle1;
    double angle2;
    bool reversed;
};



/**
 * Class for an arc entity. All angles are in Rad.
 *

 */
class LX_EXPORT PF_Arc : public PF_AtomicEntity {
public:
    PF_Arc(PF_Container* parent,
           const PF_ArcAttribute& d);
    virtual ~PF_Arc() {}

    virtual PF_Entity* clone() {
        PF_Arc* a = new PF_Arc(*this);
        a->initId();
        return a;
    }

    /**	@return LX_Define::EntityArc */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntityArc;
    }
    /** @return true */
    virtual bool isEdge() const {
        return true;
    }

    /** @return Copy of data that defines the arc. **/
    PF_ArcAttribute getData() const {
        return data;
    }
	
    virtual PF_VectorSolutions getRefPoints();

    /** Sets new arc parameters. **/
    void setData(PF_ArcAttribute d) {
        data = d;
    }

    /** @return The center point (x) of this arc */
    PF_Vector getCenter() const {
        return data.center;
    }
    /** Sets new center. */
	void setCenter(const PF_Vector& c) {
		data.center = c;
	}
	
    /** @return The radius of this arc */
    double getRadius() const {
        return data.radius;
    }
    /** Sets new radius. */
    void setRadius(double r) {
        data.radius = r;
    }

    /** @return The start angle of this arc */
    double getAngle1() const {
        return data.angle1;
    }
    /** Sets new start angle. */
	void setAngle1(double a1) {
		data.angle1 = a1;
	}
    /** @return The end angle of this arc */
    double getAngle2() const {
        return data.angle2;
    }
    /** Sets new end angle. */
	void setAngle2(double a2) {
		data.angle2 = a2;
	}
	/** 
	 * @return Direction 1. The angle at which the arc starts at 
	 * the startpoint. 
	 */
	double getDirection1() const {
		if (!data.reversed) {
			return PF_Math::correctAngle(data.angle1+M_PI/2.0);
		}
		else {
			return PF_Math::correctAngle(data.angle1-M_PI/2.0);
		}
	}
	/** 
	 * @return Direction 2. The angle at which the arc starts at 
	 * the endpoint.
	 */
	double getDirection2() const {
		if (!data.reversed) {
			return PF_Math::correctAngle(data.angle2-M_PI/2.0);
		}
		else {
			return PF_Math::correctAngle(data.angle2+M_PI/2.0);
		}
	}

    /**
     * @retval true if the arc is reversed (clockwise), 
     * @retval false otherwise 
     */
    bool isReversed() const {
        return data.reversed;
    }
	/** sets the reversed status. */
	void setReversed(bool r) {
		data.reversed = r;
	}

    /** @return Start point of the entity. */
    virtual PF_Vector getStartpoint() const {
        return startpoint;
    }
    /** @return End point of the entity. */
    virtual PF_Vector getEndpoint() const {
        return endpoint;
    }
	virtual void moveStartpoint(const PF_Vector& pos);
	virtual void moveEndpoint(const PF_Vector& pos);
	
	virtual void trimStartpoint(const PF_Vector& pos);
	virtual void trimEndpoint(const PF_Vector& pos);
	
	virtual LX_Define::Ending getTrimPoint(const PF_Vector& coord, 
	          const PF_Vector& trimPoint);

	virtual void reverse();

    PF_Vector getMiddlepoint() const;
    double getAngleLength() const;
    virtual double getLength();
    double getBulge() const;
	
    bool createFrom3P(const PF_Vector& p1, const PF_Vector& p2,
                      const PF_Vector& p3);
	bool createFrom2PDirectionRadius(const PF_Vector& startPoint, const PF_Vector& endPoint,
		double direction1, double radius);
	bool createFrom2PBulge(const PF_Vector& startPoint, const PF_Vector& endPoint,
		double bulge);

    virtual PF_Vector getNearestEndpoint(const PF_Vector& coord,
                                         double* dist = NULL);
    virtual PF_Vector getNearestPointOnEntity(const PF_Vector& coord,
            bool onEntity = true, double* dist = NULL, PF_Entity** entity=NULL);
    virtual PF_Vector getNearestCenter(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestMiddle(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     const PF_Vector& coord,
                                     double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     bool startp);

    virtual double getDistanceToPoint(const PF_Vector& coord,
                                      PF_Entity** entity=NULL,
                                      LX_Define::ResolveLevel level=LX_Define::ResolveNone,
									  double solidDist = LX_DOUBLEMAX);
    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
	virtual void moveRef(const PF_Vector& ref, const PF_Vector& offset);
    virtual void stretch(PF_Vector firstCorner,
                         PF_Vector secondCorner,
                         PF_Vector offset);

    virtual void draw(PF_Painter* painter, PF_GraphicView* view, double patternOffset=0.0);

    friend std::ostream& operator << (std::ostream& os, const PF_Arc& a);

    virtual void calculateEndpoints();
    virtual void calculateBorders();

protected:
    PF_ArcAttribute data;

    /**
     * Startpoint. This is redundant but stored for performance 
     * reasons.
     */
    PF_Vector startpoint;
    /**
     * Endpoint. This is redundant but stored for performance 
     * reasons.
     */
    PF_Vector endpoint;
};

#endif
